<?php

namespace app\common\service\order\coupon_order\movie;

use app\api\model\Movie;
use app\common\model\Sms;
use app\event\MovieNotifySuccess;
use app\model\CouponOrder;
use app\model\MovieOrder;
use app\model\SmsLog;
use think\Exception;
use think\facade\Log;

class MovieSyncOrder extends MovieBase
{

    public function syncOrder()
    {
        /**
         * @var CouponOrder $couponOrder
         */
        $couponOrder = $this->saveOrder();

        if ($couponOrder->status != $this->status) {
            $couponOrder->status = $this->status;
            $couponOrder->save();
        }

        //影票id
        if (!$couponOrder->quote_id){
            $couponOrder->quote_id = $this->params['id']??0;
            $couponOrder->save();
        }

        //结算时间
        $complate_at = date('Y-m-d H:i:s', $this->params['settle_time'] ?? 0);

        if (!empty($this->params['settle_time']) && $couponOrder->complete_at != $complate_at) {
            $couponOrder->complete_at = $complate_at;
            $couponOrder->order_finish_at = $complate_at;
            $couponOrder->save();
        }

        if ($couponOrder->profit != $this->profit) {
            $couponOrder->profit = $this->profit;
            $couponOrder->save();
        }

        if ($couponOrder->platform_profit != $this->platform_profit) {
            $couponOrder->platform_profit = $this->platform_profit;
            $couponOrder->save();
        }

        if ($couponOrder->agent_settle_status != $this->agent_settle_status) {
            $couponOrder->agent_settle_status = $this->agent_settle_status;
            $couponOrder->order_finish_at = date('Y-m-d H:i:s');
            $couponOrder->save();
        }

        if ($couponOrder->ext_data != $this->ext_data) {
            $couponOrder->log .= date('Y-m-d H:i:s') . '取票码更新';
            $couponOrder->ext_data = $this->ext_data;
            $couponOrder->save();
        }




        try {
            $value = $couponOrder->ext_data;
            /**
             * 都为空
             */
            if (empty($ticket_codes = $value['ticket_code'])) {
                return $value;
            }

            //todo 触发条件 1.查询ext_data中的ticket_code有数据  2.sms_log根据record_id去查询没有数据
            // 3.coupon_order的created_at大于影票短信版本发布 2022-08-19
            if (empty($ticket_codes)) {
                return;
            }
            $smsLog = SmsLog::query()->where('type', SmsLog::MOVIE_ORDER_TYPE)
                ->where('record_id', $couponOrder->id)
                ->find();
            if ($smsLog) {
                return;
            }
            if ($couponOrder->created_at < '2022-08-22') {
                return;
            }

            //TODO 触发发送短信
            event(new MovieNotifySuccess($couponOrder));

            $couponOrder->log.= date('Y-m-d H:i:s').'发送影票信息成功';
            $couponOrder->save();

        }catch (Exception $exception){
            $couponOrder->log.= date('Y-m-d H:i:s').'发送失败'.$exception->getMessage();
            $couponOrder->save();
        }


    }

    public function getServType()
    {
        return PRODUCT_FILM_SERV; //电影
    }


    public function getProductName()
    {

        return $this->title;

    }

    public function getProductImage()
    {
        return $this->params['film_image'] ?? '';
    }

    /**
     * 结算已完成
     * @return int|mixed
     */
    public function getAgentSettleStatus()
    {

        $settle_status = $this->params['settle_status'];

        if (in_array($settle_status, [
            MovieOrder::SETTLE_STATUS_NO,
            MovieOrder::SETTLE_STATUS_WAIT,
        ])) {
            return CouponOrder::C_AGENT_SETTLE_STATUS_NO;
        } else {
            return CouponOrder::C_AGENT_SETTLE_STATUS_SUCCESS;
        }
    }

    public function getStatus()
    {
//        2-受理中，3-待出票，4-已出票待结算，5-已结算【守兔】

        $status = $this->params['supplier_status'];

        if (in_array($status, MovieOrder::SUPPLIER_STATUS_IS_OK)) {
            return CouponOrder::C_STATUS_RECHARGE_SUCCESS;
        }

        return CouponOrder::C_STATUS_RECHARGE_FAIL;

    }
}